package com.ochnas.volunteersignup.ui;

import static com.ochnas.volunteersignup.ui.SharedConstants.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.ochnas.volunteersignup.domain.Event;
import com.ochnas.volunteersignup.domain.User;
import com.ochnas.volunteersignup.service.EventService;
import com.ochnas.volunteersignup.service.UserService;
public class MyEventsController {
	private ApplicationService applicationService;
	private UserService userService;
	private EventService eventService;
	private User user;
	private List<Event> events;
	private int size;

	public MyEventsController() {
		super();
		user = new User();
		events = new ArrayList<Event>();
	}
	
	public String listMyEvents() {
		String userId = applicationService.getRequestParameter("userId");
		try {
			user = userService.findUserAccount(Long.parseLong(userId));
			refreshEvents();
		} catch (NumberFormatException e) {
			applicationService.addErrorMessage(e.getMessage());
		}
		return "myEvents";
	}
	
	private void refreshEvents() {
		List<Event> myEvents = eventService.findEventsByCoordinator(user.getId());
		Event[] eventArray = myEvents.toArray(new Event[0]);
		Arrays.sort(eventArray, SORT_BY_ASCENDING_DATE);
		events.clear();
		events.addAll(Arrays.asList(eventArray));
		size = events.size();
	}

	public ApplicationService getApplicationService() {
		return applicationService;
	}

	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public EventService getEventService() {
		return eventService;
	}

	public void setEventService(EventService eventService) {
		this.eventService = eventService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<Event> getEvents() {
		refreshEvents();
		return events;
	}

	public void setEvents(List<Event> events) {
		this.events = events;
	}

	public int getSize() {
		refreshEvents();
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}
	
	
}
